<?php
class Training_ValidateController extends Zendvn_Controller_Action{

	public function init() {
		parent::init();
		$this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
	}
		
	public function phoneAction() {
		$input = '084-08-38.1113331212';
		$validator = new Zendvn_Validate_Phone();
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function passAction() {
		if($this->_request->isPost()){
			$pass = $this->_request->getParam('password', '');
			$passConfirm = $this->_request->getParam('passConfirm', '');
			$validator = new Zendvn_Validate_ConfirmPassword($pass);
			if(!$validator->isValid($passConfirm)){
				$message = $validator->getMessages();
				echo current($message);
				echo '<pre>';
				print_r($validator->getMessages());
				echo '</pre>';
			}
			
		}
	}

	public function multiAction() {
		//1. Textbox khong duoc rong
		//2. Do dai ki tu 3-32
		//3. Tap hop ki tu cho phep [a-zA-Z-0-9\-_\.]
		//4. User name khong duoc ton tai trong database
		$input = $this->_request->getParam('user_name', '');
		if($this->_request->isPost()){
			$pattern = '#^[a-zA-Z-0-9\-_\.]+$#';
			$options  = array('table'=>'users','field'=>'user_name');
			$validator = new Zend_Validate();
			$breakChainOnFailure = true; // False : Lỗi vấn chạy tiếp
			$validator->addValidator(new Zend_Validate_NotEmpty(), $breakChainOnFailure)
					  ->addValidator(new Zend_Validate_StringLength(3, 32), $breakChainOnFailure)
					  ->addValidator(new Zend_Validate_Regex($pattern), $breakChainOnFailure)
					  ->addValidator(new Zend_Validate_Db_NoRecordExists($options), $breakChainOnFailure);
			if(!$validator->isValid($input)){
				$message = $validator->getMessages();
				echo current($message);
//				echo '<pre>';
//				print_r($validator->getMessages());
//				echo '</pre>';
			}
		}
		
	}
	
	
	public function regexAction() {
		$input = '084-08-38.1113331212';
		$pattern = '#^084-[0-9]{2}-[0-9]{2}\.[0-9]{6}$#';
		$validator = new Zend_Validate_Regex($pattern);
//		$validator->setMessage("'%value%' does not match against pattern '%pattern%'", 'regexNotMatch');
		$validator->setMessage("'%value%' does not match against pattern 'XXX-XX-XX.XXXXX'", 'regexNotMatch');

		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function record3Action() {
		$clause = ' id != 1 ';
		//VD: Ktra xem email co ton tai trong DB khong
		$options  = array(
						'table'=>'users',
						'field'=>'user_name',
						'exclude'=> $clause
						);
		$validator = new Zend_Validate_Db_NoRecordExists($options);
		
		$input = "trieuhieu";
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function record2Action() {
		//VD: Ktra xem email co ton tai trong DB khong
		$options  = array(
						'table'=>'users',
						'field'=>'user_name',
						'exclude'=> array(
										'field'=>'id',
										'value'=>'1',
										)
						);
		$validator = new Zend_Validate_Db_NoRecordExists($options);
		
		$input = "trieuhieu";
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function recordAction() {
		//VD: Ktra xem email co ton tai trong DB khong
		$options  = array('table'=>'users','field'=>'email');
		$validator = new Zend_Validate_Db_RecordExists($options);
		
		$input = "trieuhieu509a@gmail.com";
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function intAction() {
		$input = 20.15;
		
		$validator = new Zend_Validate_Int();
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function arrayAction() {
		$arr = array('apple', 'foo', 'banana');
		
		$validator = new Zend_Validate_InArray($arr);
		if(!$validator->isValid('appleabc')){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function emptyAction() {
		$input = '';
		//Kiem tra du lieu dua vao co rong hay khong
		$validator = new Zend_Validate_NotEmpty();
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function emailAction() {
		$input = 'asdasdasd@yahoo.com.vn';
		$options = '';
		//Kiem tra du lieu dua vao co rong hay khong
		$validator = new Zend_Validate_EmailAddress();
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function betweenAction() {
		$input = '123';
		$options = '';
		//Kiem tra du lieu dua vao trong 1 khoang
		$validator = new Zend_Validate_Between(10,100);
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function digitAction() {
		$input = '12.3';
		$options = '';
		//Kiem tra du lieu dua vao co phai so nguyen k
		$validator = new Zend_Validate_Digits();
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function dateAction() {
//		$input = '12/10/2009';
//		$input = '2009/12/10';
//		$input = '32/10/2009';
		$input = '12:10:2009';
		$options = '';
		//Kiem tra du lieu dua vao co phai so nguyen k
		$validator = new Zend_Validate_Date();
		$validator->setFormat('dd-mm-YYYY');
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function greaterAction() {
		$input = '12';
		//Kiem tra du lieu dua vao co phai so nguyen k
		$validator = new Zend_Validate_GreaterThan(20);
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function lessAction() {
		$input = '22';
		//Kiem tra du lieu dua vao co phai so nguyen k
		$validator = new Zend_Validate_LessThan(20);
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo current($message);
		}
	}
	
	public function indexAction() {
		//Kiem tra chieu dai cua mot chuoi
		$input	= "this is a test";
		$validator =  new Zend_Validate_StringLength(10);
		if($validator->isValid($input)){
			echo 'Thoa man dieu kien dua vao';
		}else{
			echo 'Khong thoa man dieu kien dua vao';
		}
	}
	
	public function index2Action() {
		//Kiem tra chieu dai cua mot chuoi
		$input	= "this is a test";
		$validator =  new Zend_Validate_StringLength(5, 10);
		if($validator->isValid($input)){
			echo 'Thoa man dieu kien dua vao';
		}else{
			echo 'Khong thoa man dieu kien dua vao';
		}
	}
	
	public function index3Action() {
		//Kiem tra chieu dai cua mot chuoi
		$input	= "this is a test";
		$validator =  new Zend_Validate_StringLength(5, 10);
		if($validator->isValid($input)){
			echo 'Thoa man dieu kien dua vao';
		}else{
			$messages = $validator->getMessages();
			echo current($messages);
			echo '<pre>';
			print_r($messages);
			echo '</pre>';
		}
	}
	
	public function index4Action() {
		//Kiem tra chieu dai cua mot chuoi
		$input	= "this is a test";
		$validator =  new Zend_Validate_StringLength(5, 10);
		
		$validator->setMessages(array(
			Zend_Validate_StringLength::INVALID   => "Gia tri khong dung",
			Zend_Validate_StringLength::TOO_SHORT => "Chuoi nay qua ngan",
			Zend_Validate_StringLength::TOO_LONG  => "Chuoi nay qua dai",
		));
		
		if($validator->isValid($input)){
			echo 'Thoa man dieu kien dua vao';
		}else{
			$messages = $validator->getMessages();
			echo current($messages);
			echo '<pre>';
			print_r($messages);
			echo '</pre>';
		}
	}
	
	public function index5Action() {
		//Kiem tra chieu dai cua mot chuoi
		$input	= "this is a test";
		$validator =  new Zend_Validate_StringLength(5, 10);
		
		$validator->setMessages(array(
			Zend_Validate_StringLength::INVALID   => "Gia tri '%value%' khong dung",
			Zend_Validate_StringLength::TOO_SHORT => "Chuoi '%value%' qua ngan. Yeu cau phai tu %min% ki tu tro len",
			Zend_Validate_StringLength::TOO_LONG  => "Chuoi '%value%' qua dai.  Yeu cau phai nho hon %max% ki tu",
		));
		
		if($validator->isValid($input)){
			echo 'Thoa man dieu kien dua vao';
		}else{
			$messages = $validator->getMessages();
			echo current($messages);
			echo '<pre>';
			print_r($messages);
			echo '</pre>';
		}
	}
	
	public function index6Action() {
		//Kiem tra chieu dai cua mot chuoi
		$input	= "this is a test";
		$validator =  new Zend_Validate_StringLength(5, 10);
		
		if(!$validator->isValid($input)){
			$messages = $validator->getMessages();
			echo current($messages);
			echo '<pre>';
			print_r($messages);
			echo '</pre>';
		}
	}
}
